var college = $('#college', parent.document).html();
$(function () {
  $("#jqGrid").jqGrid({
    url: baseURL + 'TeacherInfo/Select',
    postData: {'collegeName': college},
    datatype: "json",
    colModel: [
      {label: '职工号', name: 'teacherId', index: 'teacher_id', width: 80, key: true},
      {label: '姓名', name: 'teacherName', index: 'teacher_name', width: 60},
      {label: '性别', name: 'teacherSexual', index: 'teacher_sexual', width: 40},
      {label: '出生日期', name: 'teacherBirthday', index: 'teacher_birthday', width: 80},
      {label: '所属院校', name: 'teacherCollege', index: 'teacher_college', width: 80},
      {label: '兼职院校', name: 'teacherPartTimeCollege', index: 'teacher_part_time_college', width: 80},
      {label: '教师类型', name: 'teacherType', index: 'teacher_type', width: 80}
    ],
    viewrecords: true,
    height: '100%',
    rowNum: 10,
    rowList: [10, 30, 50],
    rownumbers: true,
    // rownumWidth: 25,
    autowidth: true,
    multiselect: true,
    pager: "#jqGridPager",
    jsonReader: {
      root: "page.list",
      page: "page.currPage",
      total: "page.totalPage",
      records: "page.totalCount"
    },
    prmNames: {
      page: "page",
      rows: "limit",
      order: "order"
    },
    gridComplete: function () {
      //隐藏grid底部滚动条
      $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "srcoll"});
    },
    ondblClickRow: function (rowid) {
      var rowData = $("#jqGrid").jqGrid('getRowData', rowid);
      var rowName = $("#jqGrid").jqGrid('getGridParam', 'colNames');
      var s = "";
      var i = 2;
      for (var key in rowData) {
        s = s + rowName[i++] + ':' + rowData[key] + '<br> ';
      }
      alert(s);
    }
  });
  $("#exportExcel").attr('href', baseURL + 'TeacherInfo/export?token=' + token);
  $.ajax({
    url: baseURL + "/college/getAllcollege",
    data: {},
    success: function (r) {
      $("#teacherCollege").append("<option value=''>全校</option>");
      $("#teacherPartTimeCollege").append("<option value=''>全校</option>");
      for (i = 0; i < r.list.length; i++) {
        var tname = r.list[i].collegeName;
        var tid = r.list[i].collegeName;
        $("#teacherCollege").append("<option value='" + tid + "'>" + tname + "</option>");
        $("#teacherPartTimeCollege").append("<option value='" + tid + "'>" + tname + "</option>");
      }
    }
  });
});

var vm = new Vue({
  el: '#rrapp',
  data: {
    q: {
      key: null
    },
    showList: true,
    title: null,
    TeacherInfo: {
      teacherId: '',
      teacherName: '',
      teacherSexual: '',
      teacherBirthday: '',
      teacherCollege: '',
      teacherPartTimeCollege: '',
      teacherType: '外聘教师'
    },
    teacherId: null
  },
  methods: {
    query: function () {
      $("#exportExcel").attr('href', baseURL + 'TeacherInfo/export?token=' + token + "&teacherName=" + vm.q.key);

      vm.reload();
    },
    add: function () {
      vm.showList = false;
      vm.title = "新增";
      vm.TeacherInfo = {};
    },
    update: function () {
      var id = getSelectedRow();
      if (id == null) {
        return;
      }
      $.get(baseURL + "TeacherInfo/Info/" + id, function (r) {
        vm.showList = false;
        vm.title = "修改";
        vm.TeacherInfo = r.TeacherInfo;
        vm.teacherId = r.TeacherInfo.teacherId;
      });
    },
    del: function () {
      var ids = getSelectedRows();
      if (ids == null) {
        return;
      }
      confirm('确定要删除选中的记录？', function () {
        $.ajax({
          type: "POST",
          url: baseURL + "TeacherInfo/Delete",
          contentType: "application/json",
          data: JSON.stringify(ids),
          success: function (r) {
            if (r.code == 0) {
              alert('操作成功', function () {
                vm.reload();
              });
            } else {
              alert(r.msg);
            }
          }
        });
      });
    },
    pullin: function () {
      confirm('确定要开始数据导入？', function () {
        $.ajax({
          type: "POST",
          url: baseURL + "Pullin/getTeacherInfoData",
          success: function (r) {
            if (r.code == 0) {
              alert('操作成功', function () {
                vm.reload();
              });
            } else {
              alert(r.msg);
            }
          }
        });
      });
    },
    saveOrUpdate: function () {
      if (vm.validator()) {
        return;
      }
      var url = vm.TeacherInfo.teacherId == vm.teacherId ? "TeacherInfo/Update" : "TeacherInfo/Insert";
      $.ajax({
        type: "POST",
        url: baseURL + url,
        contentType: "application/json",
        data: JSON.stringify(vm.TeacherInfo),
        success: function (r) {
          if (r.code === 0) {
            alert('操作成功', function () {
              vm.reload();
            });
          } else {
            alert(r.msg);
          }
        }
      });
    },
    reload: function () {
      vm.showList = true;
      var page = $("#jqGrid").jqGrid('getGridParam', 'page');
      $("#jqGrid").jqGrid('setGridParam', {
        postData: {'teacherName': vm.q.key, collegeName: college},
        page: page
      }).trigger("reloadGrid");
    },
    validator: function () {
      if (isBlank(vm.TeacherInfo.teacherId)) {
        alert("职工号不能为空");
        return true;
      }
      if (isBlank(vm.TeacherInfo.teacherName)) {
        alert("姓名不能为空");
        return true;
      }
      if (isBlank(vm.TeacherInfo.teacherSexual)) {
        alert("性别不能为空");
        return true;
      }
      if (isBlank(vm.TeacherInfo.teacherBirthday)) {
        alert("出生日期不能为空");
        return true;
      }
      // if (isBlank(vm.TeacherInfo.teacherTelNumber)) {
      //   alert("联系电话不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherEmailAddress)) {
      //   alert("邮箱地址不能为空");
      //   return true;
      // }
      if (isBlank(vm.TeacherInfo.teacherCollege)) {
        alert("所属院校不能为空");
        return true;
      }
      if (isBlank(vm.TeacherInfo.teacherPartTimeCollege)) {
        alert("兼职院校不能为空");
        return true;
      }
      if (isBlank(vm.TeacherInfo.teacherCollege)) {
        alert("教师类型不能为空");
        return true;
      }
      // if (isBlank(vm.TeacherInfo.teacherPosition)) {
      //   alert("职位不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherEducation)) {
      //   alert("职称不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherTitle)) {
      //   alert("学历不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherResearchDirection)) {
      //   alert("研究方向不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherPriority)) {
      //   alert("优先级不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherClassDistribution)) {
      //   alert("课时分布不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherProfessionName)) {
      //   alert("专业名称不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherGraduatedSchool)) {
      //   alert("毕业院校不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherQualifications)) {
      //   alert("是否有教师资格不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherPersonnelId)) {
      //   alert("人事职工号不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherWhetherExternal)) {
      //   alert("是否外聘不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherWhetherLaboratory)) {
      //   alert("是否实验室人员不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherWhetherOnDuty)) {
      //   alert("是否在职不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherPoliticalStatus)) {
      //   alert("政治面貌不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherEthnic)) {
      //   alert("民族不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherDegree)) {
      //   alert("学位不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherWhetherCounselor)) {
      //   alert("是否辅导员不能为空");
      //   return true;
      // }
      // if (isBlank(vm.TeacherInfo.teacherPinyinShort)) {
      //   alert("拼音简写不能为空");
      //   return true;
      // }
    }
  }
});